package com.study.zookeeperAPI;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * 异步创建节点
 */
public class CreateNodesAsync implements Watcher {


    private static CountDownLatch latch = new CountDownLatch(1);

    private static String connectStr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2184";

    public static void main(String[] args) {


        try{
            ZooKeeper zooKeeper = new ZooKeeper(connectStr, 5000, new CreateNodesAsync());
            latch.await();
            zooKeeper.create("/zk-test-ephemeral-", "".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, new IStringCallBack(), "I am context.");
            zooKeeper.create("/zk-test-ephemeral-", "".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, new IStringCallBack(), "I am context.");
            zooKeeper.create("/zk-test-ephemeral-", "".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new IStringCallBack(), "I am context.");

            Thread.sleep(Integer.MAX_VALUE);


        }catch (IOException e){

        }catch (InterruptedException e){

        }

    }

    public void process(WatchedEvent watchedEvent) {

        if(Event.KeeperState.SyncConnected.equals(watchedEvent.getState())){

            System.out.println("connect success！！！！");
            latch.countDown();
        }
    }
}
